using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CError : CBaseDynamic, IComparable<CError>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CError(CError original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _errorUserId = original.ErrorUserId;
            _errorUserName = original.ErrorUserName;
            _errorQuerystring = original.ErrorQuerystring;
            _errorMessage = original.ErrorMessage;
            _errorStacktrace = original.ErrorStacktrace;
            _errorInnerMessage = original.ErrorInnerMessage;
            _errorInnerStacktrace = original.ErrorInnerStacktrace;
            _errorCreated = original.ErrorCreated;
            _errorRawUrl = original.ErrorRawUrl;
            _errorHash = original.ErrorHash;
        }

        //Protected (Datareader/Dataset)
        protected CError(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CError(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _errorId = int.MinValue;
            _errorUserId = long.MinValue;
            _errorUserName = string.Empty;
            _errorQuerystring = string.Empty;
            _errorMessage = string.Empty;
            _errorStacktrace = string.Empty;
            _errorInnerMessage = string.Empty;
            _errorInnerStacktrace = string.Empty;
            _errorCreated = DateTime.MinValue;
            _errorRawUrl = string.Empty;
            _errorHash = int.MinValue;
        }
        #endregion

        #region Members
        protected int _errorId;
        protected long _errorUserId;
        protected string _errorUserName;
        protected string _errorQuerystring;
        protected string _errorMessage;
        protected string _errorStacktrace;
        protected string _errorInnerMessage;
        protected string _errorInnerStacktrace;
        protected DateTime _errorCreated;
        protected string _errorRawUrl;
        protected int _errorHash;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public int ErrorId    {   get { return _errorId;   } }

        //Table Columns (Read/Write)
        public long ErrorUserId    {   get { return _errorUserId; } set { _errorUserId = value; }    }
        public string ErrorUserName    {   get { return _errorUserName; } set { _errorUserName = value; }    }
        public string ErrorQuerystring    {   get { return _errorQuerystring; } set { _errorQuerystring = value; }    }
        public string ErrorMessage    {   get { return _errorMessage; } set { _errorMessage = value; }    }
        public string ErrorStacktrace    {   get { return _errorStacktrace; } set { _errorStacktrace = value; }    }
        public string ErrorInnerMessage    {   get { return _errorInnerMessage; } set { _errorInnerMessage = value; }    }
        public string ErrorInnerStacktrace    {   get { return _errorInnerStacktrace; } set { _errorInnerStacktrace = value; }    }
        public DateTime ErrorCreated    {   get { return _errorCreated; } set { _errorCreated = value; }    }
        public string ErrorRawUrl    {   get { return _errorRawUrl; } set { _errorRawUrl = value; }    }
        public int ErrorHash    {   get { return _errorHash; } set { _errorHash = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Errors";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "ErrorId DESC";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CError other) {   return this.ErrorId.CompareTo(other.ErrorId) *-1;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "ErrorId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _errorId; }
              set { _errorId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CError(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CError(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CErrorList();               }
        protected override IList MakeList(int capacity)     {   return new CErrorList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _errorId = CAdoData.GetInt(dr, "ErrorId");
            _errorUserId = CAdoData.GetLong(dr, "ErrorUserId");
            _errorUserName = CAdoData.GetStr(dr, "ErrorUserName");
            _errorQuerystring = CAdoData.GetStr(dr, "ErrorQuerystring");
            _errorMessage = CAdoData.GetStr(dr, "ErrorMessage");
            _errorStacktrace = CAdoData.GetStr(dr, "ErrorStacktrace");
            _errorInnerMessage = CAdoData.GetStr(dr, "ErrorInnerMessage");
            _errorInnerStacktrace = CAdoData.GetStr(dr, "ErrorInnerStacktrace");
            _errorCreated = CAdoData.GetDate(dr, "ErrorCreated");
            _errorRawUrl = CAdoData.GetStr(dr, "ErrorRawUrl");
            _errorHash = CAdoData.GetInt(dr, "ErrorHash");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _errorId = CAdoData.GetInt(dr, "ErrorId");
            _errorUserId = CAdoData.GetLong(dr, "ErrorUserId");
            _errorUserName = CAdoData.GetStr(dr, "ErrorUserName");
            _errorQuerystring = CAdoData.GetStr(dr, "ErrorQuerystring");
            _errorMessage = CAdoData.GetStr(dr, "ErrorMessage");
            _errorStacktrace = CAdoData.GetStr(dr, "ErrorStacktrace");
            _errorInnerMessage = CAdoData.GetStr(dr, "ErrorInnerMessage");
            _errorInnerStacktrace = CAdoData.GetStr(dr, "ErrorInnerStacktrace");
            _errorCreated = CAdoData.GetDate(dr, "ErrorCreated");
            _errorRawUrl = CAdoData.GetStr(dr, "ErrorRawUrl");
            _errorHash = CAdoData.GetInt(dr, "ErrorHash");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("ErrorId", CAdoData.NullVal(_errorId));
            data.Add("ErrorUserId", CAdoData.NullVal(_errorUserId));
            data.Add("ErrorUserName", CAdoData.NullVal(_errorUserName));
            data.Add("ErrorQuerystring", CAdoData.NullVal(_errorQuerystring));
            data.Add("ErrorMessage", CAdoData.NullVal(_errorMessage));
            data.Add("ErrorStacktrace", CAdoData.NullVal(_errorStacktrace));
            data.Add("ErrorInnerMessage", CAdoData.NullVal(_errorInnerMessage));
            data.Add("ErrorInnerStacktrace", CAdoData.NullVal(_errorInnerStacktrace));
            data.Add("ErrorCreated", CAdoData.NullVal(_errorCreated));
            data.Add("ErrorRawUrl", CAdoData.NullVal(_errorRawUrl));
            data.Add("ErrorHash", CAdoData.NullVal(_errorHash));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CErrorList SelectAll()                                                          {    return (CErrorList)base.SelectAll();                              }
        public new CErrorList SelectAll(string orderBy)                                            {    return (CErrorList)base.SelectAll(orderBy);                       }
        public new CErrorList SelectWhere(string colName, ESign sign, object colValue)             {    return (CErrorList)base.SelectWhere(colName, sign, colValue);     }
        public new CErrorList SelectWhere(CCriteria where)                                         {    return (CErrorList)base.SelectWhere(where);                       }
        public new CErrorList SelectWhere(CCriteriaList where)                                     {    return (CErrorList)base.SelectWhere(where);                       }
        public new CErrorList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CErrorList)base.SelectWhere(where, tableOrJoin);          }
        public new CErrorList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CErrorList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CErrorList SelectWhere(string unsafeWhereClause)                                {    return (CErrorList)base.SelectWhere(unsafeWhereClause);           }
        public     CErrorList SelectById(int errorId)                              {    return (CErrorList)base.SelectById(errorId);       }
        public     CErrorList SelectByIds(List<int> ids)                        {    return (CErrorList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CErrorList SelectAll(  CPagingInfo pi)                                              {    return (CErrorList)base.SelectAll(  pi);                              }
        public new CErrorList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CErrorList)base.SelectWhere(pi, name, sign, value);           }
        public new CErrorList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CErrorList)base.SelectWhere(pi, criteria);                    }
        public new CErrorList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CErrorList)base.SelectWhere(pi, criteria);                    }
        public new CErrorList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CErrorList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CErrorList SelectByIds(CPagingInfo pi, List<int> ids)            {    return (CErrorList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CErrorList SelectAll(                                                                                           IDbTransaction tx)  {    return (CErrorList)base.SelectAll(tx);                                                     }
        public new CErrorList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CErrorList)base.SelectAll(orderBy,                                         tx);    }
        public new CErrorList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CErrorList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CErrorList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(               criteria,                       tx);    }
        public new CErrorList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(               criteria,                       tx);    }
        public new CErrorList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CErrorList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CErrorList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CErrorList SelectById(int errorId,                                                              IDbTransaction tx)  {    return (CErrorList)base.SelectById(errorId,                         tx);    }
        public     CErrorList SelectByIds(List<int> ids,                                                        IDbTransaction tx)  {    return (CErrorList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CErrorList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName,             txOrNull);  }
        public new CErrorList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CErrorList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CErrorList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CErrorList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CErrorList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CErrorList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CErrorList MakeList(DataSet              ds) { return (CErrorList)base.MakeList(ds);        }
        protected new CErrorList MakeList(DataTable            dt) { return (CErrorList)base.MakeList(dt);        }
        protected new CErrorList MakeList(DataRowCollection  rows) { return (CErrorList)base.MakeList(rows);      }
        protected new CErrorList MakeList(IDataReader          dr) { return (CErrorList)base.MakeList(dr);        }
        protected new CErrorList MakeList(object           drOrDs) { return (CErrorList)base.MakeList(drOrDs);    }        
        protected new CErrorList MakeList(byte[]             gzip) { return (CErrorList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CErrorList SelectByUserId(long errorUserId)    {    return SelectWhere(new CCriteriaList("ErrorUserId", errorUserId));    }

        //Paged
        public CErrorList SelectByUserId(CPagingInfo pi, long errorUserId)    {    return SelectWhere(pi, new CCriteriaList("ErrorUserId", errorUserId));    }

        //Count
        public int SelectCountByUserId(long errorUserId)   {   return SelectCount(new CCriteriaList("ErrorUserId", errorUserId));     }

        //Transactional
        public CErrorList SelectByUserId(long errorUserId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ErrorUserId", errorUserId), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "ErrorId", this.ErrorId);
            Store(w, "ErrorUserId", this.ErrorUserId);
            Store(w, "ErrorUserName", this.ErrorUserName);
            Store(w, "ErrorQuerystring", this.ErrorQuerystring);
            Store(w, "ErrorMessage", this.ErrorMessage);
            Store(w, "ErrorStacktrace", this.ErrorStacktrace);
            Store(w, "ErrorInnerMessage", this.ErrorInnerMessage);
            Store(w, "ErrorInnerStacktrace", this.ErrorInnerStacktrace);
            Store(w, "ErrorCreated", this.ErrorCreated);
            Store(w, "ErrorRawUrl", this.ErrorRawUrl);
            Store(w, "ErrorHash", this.ErrorHash);
        }
        #endregion

    }
}
